package cn.com.callback

import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.time.Instant
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @Title: aipHSTRecycle_operation_timing
 * @Description:华盛证券 回收
 * @author: xrm
 * @date 2022-12-20 15:52:00
 *
 */

public class aipHSTRecycle_operation_timing implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(aipHSTRecycle_operation_timing.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        String inData = "";
        String outData = "";
        String totalData = "";
        String dataSourceID = "dataSource";


        try {
            List hst_recycle_ruleslist = eliteBean.chaxun_sql("select recovery_guid,xc_recovery_time from hst_recycle_rules where 1=1 and NOW()>= take_effectstarttime and NOW()<=take_effectendtime", "dataSource");//查询回收规则
            log.debug("hst_recycle_ruleslist:" + hst_recycle_ruleslist.size);
            if (hst_recycle_ruleslist.size() > 0) {
                int arrInt = hst_recycle_ruleslist.size();
                for (int i = 0; i < arrInt; i++) {
                    String recovery_guid = hst_recycle_ruleslist.get(i).get("recovery_guid");//回收规则id
                    String xc_recovery_time = hst_recycle_ruleslist.get(i).get("xc_recovery_time");//下次回收时间

                    List nowlist = eliteBean.chaxun_sql("select DATE_FORMAT('" + xc_recovery_time + "','%Y-%m-%d') as nowtime", "dataSource");//获取下次回收时间年月日
                    def nowtime = nowlist.get(0).get("nowtime");

                    List nowlist2 = eliteBean.chaxun_sql("select DATE_FORMAT(NOW(),'%Y-%m-%d') as nowtime2", "dataSource");//获取现在时间年月日
                    def nowtime2 = nowlist2.get(0).get("nowtime2");

                    if (KHUtil.isNull(xc_recovery_time) || nowtime == nowtime2) {//下次回收时间为空或者等于此时执行
                        String sign_time = Instant.now().getEpochSecond();
                        log.debug("解析信息-" + "sign_time: " + sign_time);
                        String injson = "{\"recovery_guid\"" + ":" + "\"" + recovery_guid + "\"}"
                        log.debug("injson:" + injson);

                        totalData = Querypromocode(eliteBean, injson, dataSourceID);
                    }
                }

            }

            outData = "{\"result\":\"1\",\"message\":\"处理成功\",\"data\":[]}";


        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            outData = "{\"result\":\"-1\",\"message\":\"处理失败(" + e.getMessage() + ")\",\"data\":[]}";
        }

        return outData;
    }

    //回收查询接口 URI:	aipHSTRecycle_operation
    public static String Querypromocode(EliteBean eliteBean, String injson, String dataSourceID) {

        long startTime = System.currentTimeMillis();
        log.debug("处理开始时间:" + startTime);

        long endTime = System.currentTimeMillis();

        String reJson = null;
        String tagname = "";
        try {
            String doURL = "";

            String sysurl = KHDBConfig.getAPIURL(eliteBean, "API-InnerDomain");//配置本地接口地址

            doURL = sysurl + "/aipHSTRecycle_operation";

            log.debug("doURL:" + doURL);
            Map<String, String> headMap = new HashMap<String, String>();
            headMap.put("Content-Type", "application/x-www-form-urlencoded");
            def getResult = Util.doHttpPostUseHead(doURL, injson, "UTF-8", 10000, 0, headMap);
            log.debug("getResult:" + getResult);
        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"result\":\"-1\",\"message\":\"接口异常\",\\\"lastid\\\":\"0\"}";
        }
        return reJson;
    }

}
